package com.sheng.leetcode.year2022.month11.day08;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liusheng
 * @date 2022/11/08
 *<p>
 * 1684. 统计一致字符串的数目<p>
 *<p>
 * 给你一个由不同字符组成的字符串 allowed 和一个字符串数组 words 。<p>
 * 如果一个字符串的每一个字符都在 allowed 中，就称这个字符串是 一致字符串 。<p>
 * 请你返回 words 数组中 一致字符串 的数目。<p>
 *<p>
 * 示例 1：<p>
 * 输入：allowed = "ab", words = ["ad","bd","aaab","baa","badab"]<p>
 * 输出：2<p>
 * 解释：字符串 "aaab" 和 "baa" 都是一致字符串，因为它们只包含字符 'a' 和 'b' 。<p>
 *<p>
 * 示例 2：<p>
 * 输入：allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"]<p>
 * 输出：7<p>
 * 解释：所有字符串都是一致的。<p>
 *<p>
 * 示例 3：<p>
 * 输入：allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"]<p>
 * 输出：4<p>
 * 解释：字符串 "cc"，"acd"，"ac" 和 "d" 是一致字符串。<p>
 *<p>
 * 提示：<p>
 * 1 <= words.length <= 104<p>
 * 1 <= allowed.length <= 26<p>
 * 1 <= words[i].length <= 10<p>
 * allowed 中的字符 互不相同 。<p>
 * words[i] 和 allowed 只包含小写英文字母。<p>
 */
public class LeetCode1684 {

    @Test
    public void test01() {
//        String allowed = "ab";
//        String[] words = {"ad","bd","aaab","baa","badab"};
//        String allowed = "abc";
//        String[] words = {"a","b","c","ab","ac","bc","abc"};
        String allowed = "cad";
        String[] words = {"cc","acd","b","ba","bac","bad","ac","d"};
        System.out.println(new Solution().countConsistentStrings(allowed, words));
    }
}
class Solution {
    public int countConsistentStrings(String allowed, String[] words) {
        int ans = 0;
        List<Character> list = new ArrayList<>();
        for (char c : allowed.toCharArray()) {
            if (!list.contains(c)) {
                list.add(c);
            }
        }
        for (int i = 0; i < words.length; i++) {
            boolean flag = true;
            for (char c : words[i].toCharArray()) {
                if (!list.contains(c)) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                ans++;
            }
        }
        return ans;
    }
}
